package cn.vcjf.weapp.api.payment.qfwxpay.service;

import cn.vcjf.weapp.api.payment.qfwxpay.service.QfApi.QfPayAuthUtil;
import cn.vcjf.weapp.common.util.SysParamUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther: zhengzihang
 * @Date: 2018/7/12 11:12
 * @Description: 钱方微信公众号 支付
 */

public abstract class AbstractQfWxPayAuth<T> extends HttpServlet {

	private Logger logger = LogManager.getLogger(AbstractQfWxPayAuth.class);

	/*
	http://qfpay.com/modifypwd  账号：13559623218 密码：vcjf666888
	成本：0.5%
	商户ID：2373349
	 */

	public static String BASE_URL = SysParamUtil.getParam("base.common.url");//"http://vcjf.s1.natapp.cc/";//

	protected static String MERCH_ID = "5DgevUnBV1";//2373349

	public static String APP_CODE = "E85AB5AF2943441D80FB838B6C228640";
	//开发者密码
	public static String KEY = "8A920BE9718C414982EB5950216429C0";

	public static String PAY_PAGE_URL = "https://o2.qfpay.com/q/direct";

	public void updateCodeAndKey(String code, String key) {
		APP_CODE = code;
		KEY = key;
	}

	/**
	 * 设置子商户id
	 */
	public abstract String getMerchId();

	/**
	 * 下单参数校验
	 *
	 * @param request
	 */
	protected abstract void doCheckRequestParams(HttpServletRequest request);

	/**
	 * 下单url
	 *
	 * @return
	 */
	protected abstract String getUnifiedOrderUrl();

	/**
	 * 下单操作
	 *
	 * @param openId
	 * @param queryString
	 * @return
	 */
	public abstract void doUnifiedOrder(HttpServletResponse response, String openId, String queryString) throws IOException;

	/**
	 * 当前servlet的url,用来授权重定向
	 *
	 * @return
	 */
	protected abstract String getThisServletUrl();

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		setHttpServletContent(request, response);
		String queryString = request.getQueryString();
		logger.info("钱方微信公众号下单请求参数:{}", queryString);
		//检查参数
		try {
			doCheckRequestParams(request);

			String openId = QfPayAuthUtil.getCode(request, response,
					APP_CODE,
					getMerchId(),
					KEY
					, BASE_URL + getThisServletUrl() + "?" + queryString);
			if ("".equals(openId)) {
				response.getWriter().print("钱方获取openId失败");
			}
			if (openId != null) {
				//如果下单地址不为空,则重定向到下单地址进行下单,否则执行下单操作
//				if (StringUtils.isNotBlank(getUnifiedOrderUrl())) {
//					response.sendRedirect(BASE_URL + getUnifiedOrderUrl() + "?openId=" + openId + "&" + queryString);
//				} else {
					System.out.println("发起支付下单............");
					doUnifiedOrder(response, openId, queryString);
//				}
			}
		} catch (Exception e) {
			logger.error("钱方微信公众号下单请求异常:{}", e.getMessage());
			response.getWriter().print("钱方微信公众号下单请求失败:" + e.getMessage());
		}
		return;
	}

	public void setHttpServletContent(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=utf-8");
	}
}
